﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Web.Profile;

using JRO;

namespace WebSite.admin
{
    public partial class admin_db : System.Web.UI.Page
    {
        // Fields

        public HtmlForm form1;
        public ImageButton ImageButton1;
        public ImageButton ImageButton2;
        public ImageButton ImageButton3;
        public TextBox TextBox1;
        public TextBox TextBox2;
        public TextBox TextBox3;
        public TextBox TextBox4;
        public TextBox TextBox5;
        public TextBox TextBox6;

        // Methods
        public void Compact(string mdbPath)
        {
            string sourceFileName = ((DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()) + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString()) + DateTime.Now.Second.ToString() + ".bak";
            sourceFileName = mdbPath.Substring(0, mdbPath.LastIndexOf(@"\") + 1) + sourceFileName;
            string destconnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sourceFileName;
            string sourceConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;
            new JetEngineClass().CompactDatabase(sourceConnection, destconnection);
            File.Copy(sourceFileName, mdbPath, true);
            File.Delete(sourceFileName);
        }

        public void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (!Directory.Exists(base.Request.PhysicalApplicationPath + @"\" + re.replace(this.TextBox2.Text)))
            {
                Directory.CreateDirectory(base.Request.PhysicalApplicationPath + @"\" + re.replace(this.TextBox2.Text));
            }
            if (!File.Exists(base.Server.MapPath("../" + re.replace(this.TextBox1.Text))))
            {
                base.Response.Write("<script language=javascript>alert('当前数据库不存在');window.history.go(-1);</script>");
                base.Response.End();
            }
            File.Copy(base.Server.MapPath("../" + re.replace(this.TextBox1.Text)), base.Request.PhysicalApplicationPath + @"\Databackcopy\" + re.replace(this.TextBox3.Text), true);
            base.Response.Write("<script language=javascript>alert('数据库备份成功');window.location.href='admin_db.aspx';</script>");
        }

        public void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            if (!File.Exists(base.Server.MapPath("../" + re.replace(this.TextBox4.Text))))
            {
                base.Response.Write("<script language=javascript>alert('备份数据库不存在');window.history.go(-1);</script>");
                base.Response.End();
            }
            File.Copy(base.Server.MapPath("../" + re.replace(this.TextBox4.Text)), base.Server.MapPath("../" + re.replace(this.TextBox5.Text)), true);
            base.Response.Write("<script language=javascript>alert('数据库备份成功');window.location.href='admin_db.aspx';</script>");
        }

        public void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {
            if (!File.Exists(base.Server.MapPath("../" + re.replace(this.TextBox6.Text))))
            {
                base.Response.Write("<script language=javascript>alert('压缩数据库不存在');window.history.go(-1);</script>");
                base.Response.End();
            }
            this.Compact(base.Server.MapPath("../" + re.replace(this.TextBox6.Text)));
            base.Response.Write("<script language=javascript>alert('数据库压缩成功');window.location.href='admin_db.aspx';</script>");
        }

        public void Page_Load(object sender, EventArgs e)
        {
            if (this.Session["Adminlogin"] == null)
            {
                base.Response.Write("<script language=javascript>window.top.location.href='login.aspx';</script>");
                base.Response.End();
            }
        }

        // Properties
        public global_asax ApplicationInstance
        {
            get
            {
                return (global_asax)this.Context.ApplicationInstance;
            }
        }

        public DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }

    }
}